capture log close
clear all
set more off
pause off
# delimit;

local output "";
local log "";
local data "";


/* 
Create Figure A3.4D
*/

use "`data'\collapse_combine.dta", clear;


/* ******************************* */
/* **** DATA PREP AND RESHAPE **** */
/* ******************************* */

drop 	
	earn10yr_male earn10yr_race_w earn10yr_race_h earn10yr_race_b earn10yr_race_a earn10yr_race_o earn10yr_earnyrnum60 earn10yr_earnavg25to55
	earn55_male earn55_race_w earn55_race_h earn55_race_b earn55_race_a earn55_race_o earn55_earnyrnum60 earn55_earnavg25to55;

/* Create fractions of cohorts instead of counts (for flow variable, use single event denominator) */
foreach set in earn10yr {;
foreach stub in `set'_lfexit_flow `set'_lfexit_one `set'_lfexit_one_1 `set'_lfexit_one_rbst `set'_claim_phusold `set'_claim_phusany `set'_claim_mbaold `set'_claim_mbaany `set'_earndelt55to60 {;
	egen total_`stub'=total(`stub'), by(ser_dobyy);
};
};
foreach set in earn10yr {;
foreach stub in `set'_lfexit_one `set'_lfexit_one_1 `set'_lfexit_one_rbst `set'_claim_phusold `set'_claim_phusany `set'_claim_mbaold `set'_claim_mbaany `set'_earndelt55to60 {;	
	rename `stub' ct_`stub';
	gen `stub'=ct_`stub'/total_`stub' if ct_`stub'>10;
	replace `stub'=. if ct_`stub'<=10;
};
	rename `set'_lfexit_flow ct_`set'_lfexit_flow;
	gen `set'_lfexit_flow=ct_`set'_lfexit_flow/total_`set'_lfexit_one;
	replace `set'_lfexit_flow=. if ct_`set'_lfexit_flow<=10;
};
drop total_* ct_*;

/* Age */
gen age=year-ser_dobyy;
replace age=99 if year==9999;
drop if age<50;

/* Reshape for plot */
drop 
	year 
	earn10yr_lfexitflow_frac earn10yr_hzlast_frac earn10yr_hzfirst_frac 
	earn55_lfexit_flow earn55_earndelt55to60 earn55_lfexitflow_frac earn55_hzlast_frac earn55_hzfirst_frac 
	earn55_lfexit_one earn55_claim_phusold earn55_claim_phusany earn55_claim_mbaold earn55_claim_mbaany 
	earn55_lfexit_one_1 earn55_lfexit_one_rbst  earn55_restat_num_rflow ;
reshape wide 
	earn10yr_lfexit_flow earn10yr_lfexit_one earn10yr_lfexit_one_1 earn10yr_lfexit_one_rbst 
	earn10yr_claim_phusold earn10yr_claim_phusany earn10yr_claim_mbaold earn10yr_claim_mbaany earn10yr_earndelt55to60
	earn10yr_restat_num_rflow
, i(age) j(ser_dobyy);


/* ***************************************************** */
/* **** PLOT RETIREMENT AND CLAIMING ACROSS COHORTS **** */
/* ***************************************************** */
local color_lfexit_rbst "orange";

keep if age>=55 & age<69 ;

foreach set in earn10yr {;
foreach stub in lfexit_rbst {;
twoway 	
	connected `set'_`stub'1938 age if age>=55 & age<69, lcolor(`color_`stub''*2) lwidth(medthick) lpattern(solid) mcolor(`color_`stub''*2) msymbol(T) ||
	connected `set'_`stub'1940 age if age>=55 & age<69, lcolor(`color_`stub''*1.5) lwidth(medthick) lpattern(longdash) mcolor(`color_`stub''*1.5) msymbol(O) ||
	connected `set'_`stub'1942 age if age>=55 & age<69, lcolor(`color_`stub''*1) lwidth(medthick) lpattern(shortdash) mcolor(`color_`stub''*1) msymbol(D) ||
	connected `set'_`stub'1944 age if age>=55 & age<69, lcolor(`color_`stub''*.75) lwidth(medthick) lpattern(shortdash) mcolor(`color_`stub''*.75) msymbol(S) ||
	connected `set'_`stub'1946 age if age>=55 & age<69, lcolor(`color_`stub''*.5) lwidth(medthick) lpattern(longdash) mcolor(`color_`stub''*.5) msymbol(Th) ||
	connected `set'_`stub'1948 age if age>=55 & age<69, lcolor(`color_`stub''*.25) lwidth(medthick) lpattern(solid) mcolor(`color_`stub''*.25) msymbol(Oh)
	xtitle("Age", size(medsmall)) xsc(r(55 68)) xlab(55(1)68)
	ytitle("Fraction of birth cohort", size(medsmall)) /*ysc(r(`ysclow_`stub'' .1)) ylab(`ysclow_`stub''(.1).1)*/
	legend(col(3) row(2) label(1 "YOB1938") label(2 "YOB1940") label(3 "YOB1942") label(4 "YOB1944") label(5 "YOB1946") label(6 "YOB1948"))
	graphregion(color(white))
	bgcolor(white)
	ylabel(,nogrid)
	;
	graph export "`output'\\Figure_A34D.png", replace;
};
};
